package com.ronsai.umtrack.base;

import android.content.Context;

import com.ronsai.umtrack.api.ObserverResponseListener;
import com.ronsai.umtrack.api.ProgressObserver;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class BaseModel<T> {

    public void subscribe(Context context, Observable observable, ObserverResponseListener<T> listener) {
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressObserver<T>(context) {
                    @Override
                    public void onSuccess(T t) {
                        listener.onSuccess(t);
                    }

                    @Override
                    public void onError(int code, String msg) {
                        listener.onError(code, msg);
                    }
                });
    }

}
